package com.nlscan.qdkit.ui.publico.util;

import com.quvii.qvlib.util.LogUtil;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class RxJavaUtils {


    public static void Wait(int seconds, WaitCallBack callBack) {
        Observable.timer(seconds, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Long aLong) {
                        callBack.onWait();
                    }

                    @Override
                    public void onError(Throwable e) {
                        LogUtil.e("onError " + e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public static void Wait(long milliSeconds, WaitCallBack callBack) {
        Observable.timer(milliSeconds, TimeUnit.MILLISECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Long aLong) {
                        callBack.onWait();
                    }

                    @Override
                    public void onError(Throwable e) {
                        LogUtil.e("onError " + e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public interface WaitCallBack {
        void onWait();
    }
}
